package com.hghivln.erpwarehouse.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.entity.Goods;
import com.hghivln.erpwarehouse.business.entity.Inport;
import com.hghivln.erpwarehouse.business.entity.Provider;
import com.hghivln.erpwarehouse.business.mapper.InportMapper;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.service.InportService;
import com.hghivln.erpwarehouse.business.service.ProviderService;
import com.hghivln.erpwarehouse.business.vo.InportVo;
import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@Service
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProviderService providerService;

    @Override
    public DataGridlView QueryAllInport(InportVo inportVo) {
        Page<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        QueryWrapper<Inport> qw = new QueryWrapper<>();
        qw.eq(!StringUtils.isEmpty(inportVo.getGoodsid()),"goodsid",inportVo.getGoodsid());
        qw.eq(!StringUtils.isEmpty(inportVo.getProviderid()),"providerid",inportVo.getProviderid());
        qw.ge(inportVo.getStartDate()!=null,"inporttime",inportVo.getStartDate());
        qw.le(inportVo.getEndDate()!=null,"inporttime",inportVo.getEndDate());
        getBaseMapper().selectPage(page, qw);
        List<Inport> records = page.getRecords();
        for (Inport inport : records) {
            if (inport.getGoodsid() != null){
                Goods goods = goodsService.getById(inport.getGoodsid());
                inport.setSize(goods.getSize());
                inport.setGoodsname(goods.getGoodsname());
            }
            if (inport.getProviderid() != null) {
                Provider provider = providerService.getById(inport.getProviderid());
                inport.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridlView(page.getTotal(),page.getRecords());
    }

    @Override
    public Inport addInport(Inport inport) {
        Subject subject = SecurityUtils.getSubject();
        ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
        inport.setOperateperson(activerUser.getUser().getName());
        inport.setInporttime(new Date());
        getBaseMapper().insert(inport);
        // 更新库存
        Goods goods = goodsService.getById(inport.getGoodsid());
        goods.setNumber(goods.getNumber() + inport.getNumber());
        goodsService.updateGoods(goods);
        return inport;
    }

    @Override
    public boolean updateById(Inport entity) {
        return super.updateById(entity);
    }
}
